#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : tools-for-internet-security
@File : hash_verify.py
@Author : Hunter
@Time : 2025/5/15 17:39  
@脚本说明 : 
"""
import hashlib


def generate_md5(file_path):
    with open(file_path, "rb") as f:
        return hashlib.md5(f.read()).hexdigest()


def generate_sha1(file_path):
    with open(file_path, "rb") as f:
        return hashlib.sha1(f.read()).hexdigest()


def generate_sha256(file_path):
    with open(file_path, "rb") as f:
        return hashlib.sha256(f.read()).hexdigest()


def generate_hash(file_path):
    md5 = generate_md5(file_path)
    sha1 = generate_sha1(file_path)
    sha256 = generate_sha256(file_path)
    print(f"该文件的哈希值：")
    print(f"MD5: {md5}")
    print(f"SHA1: {sha1}")
    print(f"SHA256: {sha256}")


def verify_hash(file_path, hash_value: str):
    """
    哈希校验
    :param file_path: 文件路径
    :param hash_value: 哈希值，支持MD5/SHA1/SHA256
    """
    if len(hash_value) == 32:
        if hash_value == generate_md5(file_path):
            print("[+] MD5校验成功")
        else:
            print("[-] MD5校验失败")
    elif len(hash_value) == 40:
        if hash_value == generate_sha1(file_path):
            print("[+] SHA1校验成功")
        else:
            print("[-] SHA1校验失败")
    elif len(hash_value) == 64:
        if hash_value == generate_sha256(file_path):
            print("[+] SHA256校验成功")
        else:
            print("[-] SHA256校验失败")
    else:
        print("哈希值输入有误，请检查！")
        return


if __name__ == '__main__':
    # generate_hash("../测试文档.txt")
    verify_hash("../测试文档.txt", "a83dae4e4442ee4f78ca36f9cef7be32b9e39c0337269a198d2be47cf08ae92b")